package com.yao.component.circle;

import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.yao.sdk.net.NetConstant;
import com.yao.sdk.util.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Circle Photo 列表的数据适配器
 */
public class PhotoListAdapter extends RecyclerView.Adapter<PhotoListAdapter.ViewHolder> {



    private ArrayList<String> imageList = new ArrayList<>();


    public void setData(ArrayList<String> imageList) {
        if (Utils.isEmpty(imageList)) {
            return;
        }
        this.imageList = imageList;
    }

    /**
     * 通过ViewHolder可以访问每个item的所有控件
     */
    public static class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        public ImageView circlePhoto;

        public ViewHolder(View v) {
            super(v);
            circlePhoto = (ImageView)v.findViewById(R.id.circle_photo);
        }
    }


    // 创建每个Item的视图 (invoked by the item_circle manager)
    @Override
    public PhotoListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        //创建circleItem所对应的控件
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_circle_photo, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    // Replace the contents of a view (invoked by the item_circle manager)
    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {

        //final Post circleItem = imageList.get(position);
//        final String imageUri = NetConstant.BASE_IMAGE_URL + imageList.get(position);
        final String imageUri = "file://" + imageList.get(position);
        Picasso.with(holder.circlePhoto.getContext()).load(imageUri).resize(150, 150)
                .centerCrop().into(holder.circlePhoto);
        holder.circlePhoto.setTag(position);
        holder.circlePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (Integer) v.getTag();
                ArrayList<String> pictureList = new ArrayList<String>();
                String url = null;
                for (int i = 0; i < imageList.size(); i++) {
//                    url = NetConstant.BASE_IMAGE_URL + imageList.get(i);
                    url = "file://" + imageList.get(i);
                    pictureList.add(url);
                }
                Intent intent = new Intent(v.getContext(), PictureBrowseActivity.class);
                intent.putStringArrayListExtra(PictureBrowseActivity.IMAGE_URL_LIST, pictureList);
                intent.putExtra(PictureBrowseActivity.IMAGE_URL_POSITION, position);
                v.getContext().startActivity(intent);
            }
        });
    }

    // 获取Circle数据的item个数
    @Override
    public int getItemCount() {
        return imageList.size();
    }


}


